package com.hang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hang.entity.Message;
import com.hang.mapstruct.MessageMapStruct;
import com.hang.model.dto.request.MessageQueryDTO;
import com.hang.model.vo.MessageInfoVO;
import com.hang.model.vo.MessageInfoVOList;
import com.hang.service.IMessageService;
import com.hang.mapper.MessageMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 *
 */
@Service
@AllArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements IMessageService{

    private MessageMapStruct messageMapStruct;

    /**
     * 查看用户的消息列表
     * @param messageQueryDTO
     * @return
     */
    @Override
    public MessageInfoVOList getUserMessageInfoList(MessageQueryDTO messageQueryDTO) {
        Long userId = messageQueryDTO.getUserId();
        Integer messageType = messageQueryDTO.getMessageType();
        Integer isRead = messageQueryDTO.getIsRead();

        Integer pageNum = messageQueryDTO.getPageNum();
        Integer pageSize = messageQueryDTO.getPageSize();

        Page<Message> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Message> messageQueryWrapper = new QueryWrapper<>();

        messageQueryWrapper.lambda().eq(Message::getReceiveId,userId);

        //通知类型筛选
        if(messageType.compareTo(1) == 0){
            messageQueryWrapper.lambda().eq(Message::getTitle,"任务通知");
        }else if(messageType.compareTo(2) == 0){
            messageQueryWrapper.lambda().eq(Message::getTitle,"任务更新通知");
        }else if(messageType.compareTo(3) == 0){
            messageQueryWrapper.lambda().eq(Message::getTitle,"任务接取通知");
        }else if(messageType.compareTo(4) == 0){
            messageQueryWrapper.lambda().eq(Message::getTitle,"缺陷通知");
        }else if(messageType.compareTo(5) == 0){
            messageQueryWrapper.lambda().eq(Message::getTitle,"缺陷更新通知");
        }else if(messageType.compareTo(6) == 0){
            messageQueryWrapper.lambda().eq(Message::getTitle,"缺陷接收通知");
        }

        //是否已读筛选
        if(isRead.compareTo(1) == 0){
            messageQueryWrapper.lambda().eq(Message::getIsRead,0);
        }else if(isRead.compareTo(2) == 0){
            messageQueryWrapper.lambda().eq(Message::getIsRead,1);
        }

        List<Message> messageList = Optional.ofNullable(this.baseMapper.selectPage(page, messageQueryWrapper).getRecords()).orElse(new LinkedList<>());

        //转化
        List<MessageInfoVO> messageInfoVOList = messageMapStruct.messageListToMessageInfoVOList(messageList);

        MessageInfoVOList resultVO = new MessageInfoVOList();
        resultVO.setTotal(this.baseMapper.selectCount(messageQueryWrapper));
        resultVO.setMessageInfoVOList(messageInfoVOList);

        return resultVO;
    }
}




